package Registration;

import java.io.RandomAccessFile;
import java.sql.ResultSet;

import User.Uservalidation;

import control.Control;


import dbmanager.DBManager;

public class AddDetail 
{
		String Path;
		DBManager db = new DBManager();
		
		public AddDetail(String Path) 
		{
			this.Path = Path;
			db.getConnect();
		}
		public int Register()
		{
			int Count=0;
			
			try 
			{
				
				RandomAccessFile raf = new RandomAccessFile(Path,"rws");
				
				String line = null;
				line=raf.readLine();
				
				String col[] = line.split("\t"); 
				
				ResultSet rs = db.getSelect("SELECT * FROM command where fieldname='name' and msgtype = 'master_table'");
				rs.next();
				String NameChar = rs.getString("spchar");
				
				rs = db.getSelect("SELECT * FROM command where fieldname='pid' and msgtype = 'property_details'");
				rs.next();
				String pidChar = rs.getString("spchar");
				
				rs = db.getSelect("SELECT * FROM command where fieldname='pv' and msgtype = 'property_details'");
				rs.next();
				String pvChar = rs.getString("spchar");
				
				String userid = "+911234567891";
				Uservalidation getpass = new Uservalidation(db);
				String password = getpass.getPasword(userid);
				while((line=raf.readLine())!=null)
				{
					String message ="";
					try 
					{
						String[] records = line.split("\t");
						String mastername = records[0];
						
						message = NameChar+"\""+mastername+"\"";
						
						String propString = "";
						for(int i=1;i<col.length;i++)
						//	for(int i=1;i<records.length;i++)
						{
							try
							{
								String pv = records[i].trim();
								if(pv.length()==0)
									pv = "no$";
								propString = propString+pidChar+"\""+col[i].trim()+"\""+pvChar+"\""+pv+"\"";
							}
							catch (Exception e) 
							{
								propString = propString+pidChar+"\""+col[i].trim()+"\""+pvChar+"\""+"no$"+"\"";
							}
						}
						
						propString=propString+"!aan";
						System.out.println(propString);
						
						message = message + propString;
						System.out.println(message);

						message = "11 master_table add"+message+"#"+password;
						System.out.println(message);
						
						Control controlClassObj = new Control(userid,message,db);
						controlClassObj.messageProcessing();
						controlClassObj.requestProcess().toString();
						//db.pLog.addLog();
					} 
					catch (Exception e) 
					{
					}
					Count++;
				}
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
			return Count;
		}
		public static void main(String[] args) 
		{
			AddDetail reg = new AddDetail("D:\\Register\\Item\\supp.csv");
			System.out.println("Result :: "+reg.Register()+" Items are Registered in the Database");
		}
}
